package com.pirestupppai.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.imagetool.painttool.PaintView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 *  画笔页面
 */

public class ImagePaintActivity extends BaseActivity {
    @BindView(R.id.paint_view)
    PaintView mPaintView;
    @BindView(R.id.complete_btn)
    ImageView mCompleteBtn;
    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.revoke_img)
    ImageView mRevokeBtn;
    @BindView(R.id.red_layout)
    RelativeLayout mRedLayout;
    @BindView(R.id.green_layout)
    RelativeLayout mGreenLayout;
    @BindView(R.id.yellow_layout)
    RelativeLayout mYellowLayout;
    @BindView(R.id.black_layout)
    RelativeLayout mBlackLayout;
    @BindView(R.id.seekbar)
    SeekBar mPaintSizeSeekBar;

    private List<RelativeLayout> mColorLayouts;
    private int selectColorIndex;
    private String [] paintColors = new String [] {"#FC0102", "#01CC33", "#FFFC33", "#000000"};

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected void initListeners() {
        mCompleteBtn.setOnClickListener(this);
        mRevokeBtn.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);

        for (selectColorIndex = 0; selectColorIndex < mColorLayouts.size(); selectColorIndex++) {
            mColorLayouts.get(selectColorIndex).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View currentLayout) {
                    int index;
                    for (index = 0; index < mColorLayouts.size(); index++) {
                        if (currentLayout == mColorLayouts.get(index)) {
                            break;
                        }
                    }
                    HideBackgroundCircle();
                    currentLayout.setBackgroundResource(R.drawable.shape_select_color_background);
                    mPaintView.setColor(Color.parseColor(paintColors[index]));
                }
            });
        }

        mPaintSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float paintSize = (((float)progress / 50f)) * 20f;
                mPaintView.setSize((int) paintSize);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    protected void initViews() {
        mColorLayouts = new ArrayList<>();
        mColorLayouts.add(mRedLayout);
        mColorLayouts.add(mGreenLayout);
        mColorLayouts.add(mYellowLayout);
        mColorLayouts.add(mBlackLayout);
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_image_paint;
    }

    @Override
    protected void initLogic() {
        mPaintView.setBackgroundBitmap(ImageEditActivity.mDeliverBitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_btn:
                ImageEditActivity.mDeliverBitmap = mPaintView.getmBackgroundBitmap();
                setResult(RESULT_OK);
                this.finish();
                break;
            case R.id.back_btn:
                this.finish();
                break;
            case R.id.revoke_img:
                mPaintView.revokePaint();
                break;

        }
    }

    /**
     *  隐藏所有颜色按钮的背景
     */
    private void HideBackgroundCircle() {
        for (int i = 0; i < mColorLayouts.size(); i++) {
            mColorLayouts.get(i).setBackground(null);
        }
    }
}
